package com.mini.cn.goods.io.netty.io;


import com.mini.cn.goods.io.netty.ws.IWsHandel;
import com.mini.cn.goods.io.netty.ws.ParameterMap;
import com.mini.cn.goods.io.netty.ws.WebSocketNettyServer;
import com.mini.cn.goods.io.netty.ws.WebSocketSession;
import io.netty.handler.codec.http.HttpHeaders;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: fly
 * Date: 2020-01-16
 * Time: 7:18 下午
 */
public class NettyTest {

    public static void main(String[] agrs) {
        WebSocketNettyServer webSocketNettyServer = new WebSocketNettyServer(new MyWsHandle(), null);
        webSocketNettyServer.setPort(8888);
        webSocketNettyServer.addPaths(new String[]{"/ws"});
        webSocketNettyServer.addServiceListener(new IFourLayerServerStateListener() {
            @Override
            public void onServiceOnReady(IFourLayerConfig config) {
                System.out.println(config.ifReuseAddr());
            }

            @Override
            public void onServiceOnStart(int port) {

            }

            @Override
            public void onServiceOnStop(int port) {

            }
        });
        webSocketNettyServer.init().start();
    }

    public static class MyWsHandle implements IWsHandel {
        @Override
        public void onHandShakeFail(WebSocketSession session) {
            System.out.println("onHandShakeFail");
        }


        @Override
        public void onOpen(WebSocketSession session, HttpHeaders headers, ParameterMap parameterMap) throws IOException {
            System.out.println("onOpen!!!!!!!!");
        }

        @Override
        public void onBinary(WebSocketSession session, byte[] bytes) {
            System.out.println("onBinary");
        }

        @Override
        public void onEvent(WebSocketSession session, Object evt) {
        }

        @Override
        public void onMessage(WebSocketSession session, String message) {
            System.out.println("onMessage===" + message);
        }

        @Override
        public void onPingMessage(WebSocketSession session) {

        }

        @Override
        public void onClose(WebSocketSession session) throws IOException {
            System.out.println("onClose!!!!===");
        }

        @Override
        public void onError(WebSocketSession session, Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("onError==" + throwable);
        }
    }
}
